package com.yao.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.sys.entity.dto.AccountDto;
import com.yao.sys.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 账号表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-03-23
 */
@Api(tags = "账号管理")
@RestController
@RequestMapping(value = "/account")
public class AccountController {

    private static Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "新增账号", notes = "新增账号")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody AccountDto model) {
        accountService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改账号", notes = "修改账号")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody AccountDto model) {
        return ApiResponse.success();
    }

    @ApiOperation(value = "删除账号", notes = "逻辑删除账号")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {
        // 删除账号时将删除关联的用户
        ValidateHelper.validateId(id);

        return ApiResponse.success(accountService.deleteById(id));
    }

    @ApiOperation(value = "获取账号详情", notes = "获取账号详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<AccountDto> detail(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        return ApiResponse.success(accountService.getById(id));
    }

    @ApiOperation(value = "账号列表(分页)", notes = "账号列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<AccountDto>> list(@RequestBody PageQueryBean queryBean) {

        return ApiResponse.success(accountService.listpage(queryBean));
    }

}